<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="农作物名" prop="monitorsName">
        <el-input
          v-model="queryParams.monitorsName"
          placeholder="请输入农作物名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检测时间" prop="monitorsTime">
        <el-date-picker clearable
          v-model="queryParams.monitorsTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择检测时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="检测人员" prop="monitorsUser">
        <el-input
          v-model="queryParams.monitorsUser"
          placeholder="请输入检测人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生长阶段" prop="monitorsGrow">
        <el-select v-model="queryParams.monitorsGrow" placeholder="请选择生长阶段" clearable>
          <el-option
            v-for="dict in dict.type.sys_monitor_grow"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['monitors:monitors:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['monitors:monitors:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['monitors:monitors:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['monitors:monitors:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="monitorsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="监测编号" align="center" prop="monitorsId" />
      <el-table-column label="农作物名" align="center" prop="monitorsName" />
      <el-table-column label="检测时间" align="center" prop="monitorsTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.monitorsTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检测人员" align="center" prop="monitorsUser" />
      <el-table-column label="天气情况" align="center" prop="monitorsWeather">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_weather" :value="scope.row.monitorsWeather"/>
        </template>
      </el-table-column>
      <el-table-column label="生长阶段" align="center" prop="monitorsGrow">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_monitor_grow" :value="scope.row.monitorsGrow"/>
        </template>
      </el-table-column>
      <el-table-column label="叶片形态" align="center" prop="monitorsLeaf">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_monitor_leaf" :value="scope.row.monitorsLeaf"/>
        </template>
      </el-table-column>
      <el-table-column label="检测结果" align="center" prop="monitorsResult" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['monitors:monitors:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['monitors:monitors:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改监测管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="农作物名" prop="monitorsName">
          <el-input v-model="form.monitorsName" placeholder="请输入农作物名称" />
        </el-form-item>
        <el-form-item label="检测时间" prop="monitorsTime">
          <el-date-picker clearable
            v-model="form.monitorsTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择检测时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="检测人员" prop="monitorsUser">
          <el-input v-model="form.monitorsUser" placeholder="请输入检测人员" />
        </el-form-item>
        <el-form-item label="天气情况" prop="monitorsWeather">
          <el-select v-model="form.monitorsWeather" placeholder="请选择天气情况">
            <el-option
              v-for="dict in dict.type.sys_weather"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生长阶段" prop="monitorsGrow">
          <el-select v-model="form.monitorsGrow" placeholder="请选择生长阶段">
            <el-option
              v-for="dict in dict.type.sys_monitor_grow"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="叶片形态" prop="monitorsLeaf">
          <el-select v-model="form.monitorsLeaf" placeholder="请选择叶片形态">
            <el-option
              v-for="dict in dict.type.sys_monitor_leaf"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检测结果" prop="monitorsResult">
          <el-input v-model="form.monitorsResult" placeholder="请输入检测结果" />
        </el-form-item>
        <el-divider content-position="center">监测详细信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddMonitor">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteMonitor">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="monitorList" :row-class-name="rowMonitorIndex" @selection-change="handleMonitorSelectionChange" ref="monitor">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="*监测项目" prop="monitorName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.monitorName" placeholder="请输入监测项目" />
            </template>
          </el-table-column>
          <el-table-column label="监测数值(1-10)" prop="monitorNum" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.monitorNum" placeholder="请输入监测数值" />
            </template>
          </el-table-column>
          <el-table-column label="*土地情况" prop="monitorLand" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.monitorLand" placeholder="请输入土地情况" />
            </template>
          </el-table-column>
          <el-table-column label="其他情况" prop="monitorOther" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.monitorOther" placeholder="请输入其他情况" />
            </template>
          </el-table-column>
          <el-table-column label="*监测记录" prop="monitorShow" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.monitorShow" placeholder="请输入监测记录" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMonitors, getMonitors, delMonitors, addMonitors, updateMonitors } from "@/api/monitors/monitors";

export default {
  name: "Monitors",
  dicts: ['sys_monitor_grow', 'sys_weather', 'sys_monitor_leaf'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedMonitor: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 监测管理表格数据
      monitorsList: [],
      // ${subTable.functionName}表格数据
      monitorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        monitorsName: null,
        monitorsTime: null,
        monitorsUser: null,
        monitorsGrow: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        monitorsName: [
          { required: true, message: "农作物名称不能为空", trigger: "blur" }
        ],
        monitorsTime: [
          { required: true, message: "检测时间不能为空", trigger: "blur" }
        ],
        monitorsUser: [
          { required: true, message: "检测人员不能为空", trigger: "blur" }
        ],
        monitorsWeather: [
          { required: true, message: "天气情况不能为空", trigger: "change" }
        ],
        monitorsGrow: [
          { required: true, message: "生长阶段不能为空", trigger: "change" }
        ],
        monitorsLeaf: [
          { required: true, message: "叶片形态不能为空", trigger: "change" }
        ],
        monitorsResult: [
          { required: true, message: "检测结果不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询监测管理列表 */
    getList() {
      this.loading = true;
      listMonitors(this.queryParams).then(response => {
        this.monitorsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        monitorsId: null,
        monitorsName: null,
        monitorsTime: null,
        monitorsUser: null,
        monitorsWeather: null,
        monitorsGrow: null,
        monitorsLeaf: null,
        monitorsResult: null
      };
      this.monitorList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.monitorsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加监测管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const monitorsId = row.monitorsId || this.ids
      getMonitors(monitorsId).then(response => {
        this.form = response.data;
        this.monitorList = response.data.monitorList;
        this.open = true;
        this.title = "修改监测管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.monitorList = this.monitorList;
          if (this.form.monitorsId != null) {
            updateMonitors(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMonitors(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const monitorsIds = row.monitorsId || this.ids;
      this.$modal.confirm('是否确认删除监测管理编号为"' + monitorsIds + '"的数据项？').then(function() {
        return delMonitors(monitorsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** ${subTable.functionName}序号 */
    rowMonitorIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** ${subTable.functionName}添加按钮操作 */
    handleAddMonitor() {
      let obj = {};
      obj.monitorName = "";
      obj.monitorNum = "";
      obj.monitorLand = "";
      obj.monitorOther = "";
      obj.monitorShow = "";
      this.monitorList.push(obj);
    },
    /** ${subTable.functionName}删除按钮操作 */
    handleDeleteMonitor() {
      if (this.checkedMonitor.length == 0) {
        this.$modal.msgError("请先选择要删除的${subTable.functionName}数据");
      } else {
        const monitorList = this.monitorList;
        const checkedMonitor = this.checkedMonitor;
        this.monitorList = monitorList.filter(function(item) {
          return checkedMonitor.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleMonitorSelectionChange(selection) {
      this.checkedMonitor = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('monitors/monitors/export', {
        ...this.queryParams
      }, `monitors_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
